package com.scut.edu.wanyou.home.talk;

import java.util.ArrayList;
import java.util.Calendar;

import com.scut.edu.wanyou.home.ChildHolder;
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ChatManager;
import org.jivesoftware.smack.ChatManagerListener;
import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;

import com.scut.edu.wanyou.Constans;
import com.scut.edu.wanyou.home.ChatMsgEntity;
import com.scut.edu.wanyou.home.ChatMsgViewAdapter;
import com.scut.edu.wanyou.home.HomeActivity;
import com.scut.edu.wanyou.home.HomeActivity.MyListAdapter;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;

/**
 * Description:与好友聊天管理
 * ChatOne.java Create on 2013-1-10 下午1:31:52 
 * @author chenxiaochen * @version 1.0
 * Copyright (c) 2013 Company,Inc. All Rights Reserved.
 */
public class ChatOne {
	Connection connection ;
	Chat newChat;
	ChildHolder friend;
	MyListAdapter madapter ;
	ChatMsgViewAdapter cadapter = null; 
	public ChatOne(String jid)
	{
		connection = XmppTool.getConnection();
	    ChatManager chatmanager = connection.getChatManager();

        // get user
        // new a session
        newChat = chatmanager.createChat(jid, null);
        // 监听聊天消息

        chatmanager.addChatListener(new ChatManagerListenerEx());

        // send message
        
	}

    /**
     * 初始化home界面监听
     * @param adapter
     * @param friend
     */
	public void initHomeListener(MyListAdapter adapter,ChildHolder friend)
	{
		this.madapter = adapter;
		this.friend = friend;
	}

    /**
     * 初始化聊天监听
     * @param adapter
     */
	public void initChatListener(ChatMsgViewAdapter adapter)
	{
		this.cadapter = adapter;
	}

    /**
     * 发送一条消息
     * @param msg
     */
	public void SendMsg(String msg)
	{
		// send message
        try {
            newChat.sendMessage(msg);
            //Log.i("Chatone",result);
        } catch (XMPPException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
	}

    /**
     * 处理来到消息的handler
     */
	public Handler handler = new Handler() {

        @Override
        public void handleMessage(android.os.Message msg) {

            switch (msg.what) {
            case 0: {
                String result = msg.getData().getString("body");
                //只收集自己这一条的消息
                String user =  msg.getData().getString("user");
                if(user.split("/")[0].equals(friend.title) )
                {
                    //一条消息记录
                    ChatMsgEntity entity = new ChatMsgEntity();
                    entity.setDate(getDate());
                    entity.setName(friend.Id);
                    entity.setMsgType(true);
                    entity.setText(result);
                    friend.msgs.add(entity);
                    friend.msgNumUnRead++;
                    madapter.notifyDataSetChanged();
                    if(cadapter != null)
                    {
                        cadapter.notifyDataSetChanged();
                    }
                }
            }
                break;
            default:
                break;
            }
        }
    };
	public class ChatManagerListenerEx implements ChatManagerListener {

        public void chatCreated(Chat chat, boolean arg1) {
            // TODO Auto-generated method stub
        	MessageListener ml = new MessageListenerEx();
            chat.addMessageListener(ml);
        }

    }

    public class MessageListenerEx implements MessageListener 
    {
        public void processMessage(Chat arg0, Message arg1) {
            String result = arg1.getFrom() + ":" + arg1.getBody();
            Log.i("Chatone",result);
            android.os.Message msg = new android.os.Message();
            msg.what = 0;
            Bundle bd = new Bundle();
            bd.putString("msg", result);
            bd.putString("user",arg1.getFrom());
            bd.putString("body",arg1.getBody());
            msg.setData(bd);
            handler.sendMessage(msg);
        }
    }
    
    private String getDate() {
        Calendar c = Calendar.getInstance();

        String year = String.valueOf(c.get(Calendar.YEAR));
        String month = String.valueOf(c.get(Calendar.MONTH));
        String day = String.valueOf(c.get(Calendar.DAY_OF_MONTH) + 1);
        String hour = String.valueOf(c.get(Calendar.HOUR_OF_DAY));
        String mins = String.valueOf(c.get(Calendar.MINUTE));
        
       
        StringBuffer sbBuffer = new StringBuffer();
        sbBuffer.append(year + "-" + month + "-" + day + " " + hour + ":" + mins); 
        						
        						
        return sbBuffer.toString();
    }
	
}
